package com.yzz.composeweatherproject.activity

import android.util.Log
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.gestures.stopScroll
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.AbsoluteCutCornerShape
import androidx.compose.foundation.shape.CornerBasedShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import androidx.navigation.NavController
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.yzz.composeweatherproject.R
import com.yzz.composeweatherproject.activity.ui.theme.ComposeWeatherProjectTheme
import kotlinx.coroutines.flow.collect

/**
 * @Author : yangzuozhe
 * @Time : On 2022/5/29 20:27
 *
 * @Description : NavigationMoudle
 */
private const val PROFILE_KEY = "profile"
private const val FRIENDS_LIST_KEY = "friendslist"

@Composable
fun NavigationView() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = "$PROFILE_KEY?userId={userId}") {
        composable("$PROFILE_KEY?userId={userId}",
            arguments = listOf(navArgument("userId") { defaultValue = "me" })
        ) { backStackEntry ->
            Profile(navController, backStackEntry.arguments?.getString("userId"))
        }
        composable(FRIENDS_LIST_KEY) { FriendsList(navController) }
    }
}

@Composable
fun Profile(navController: NavController, userId: String? = "userinit") {
    Log.d("yyzzbottom", "pro")
    // 之前将所有内容从后堆栈弹出到“家”目的地
// 导航到“好友列表”目的地
    // 弹出所有内容，包括“家”目的地
// 导航到“friendslist”目的地之前的后退堆栈
    Button(onClick = {
        navController.navigate(FRIENDS_LIST_KEY)
    }) {
        Text(text = "Navigate next,userId=$userId")
    }
}

@Composable
fun FriendsList(navController: NavController) {
    Log.d("yyzzbottom", "fri")

    Button(onClick = {
        navController.navigate("$PROFILE_KEY?userId=user123")
    }, modifier = Modifier.fillMaxSize()) {
        Text(text = "Navigate back")
    }
}

sealed class Screen(val route: String, val resourceString: String) {
    object Profile : Screen("profile", "profile")
    object FriendsList : Screen("friendslist", "friendslist")
}

@Composable
fun BottomNavigationView() {
    val items = listOf(
        Screen.Profile,
        Screen.FriendsList,
    )

    val navController = rememberNavController()
    Scaffold(
        bottomBar = {
            BottomNavigation {
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                val currentDestination = navBackStackEntry?.destination
                items.forEach { screen ->
                    BottomNavigationItem(
                        icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
                        label = { Text(screen.resourceString) },
                        selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                        onClick = {
                            navController.navigate(screen.route) {
                                // 弹出到图表的起始目的地，以避免在用户选择项目时在后堆栈上建立大量目的地
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                // 重新选择同一个项目时避免同一个目的地的多个副本
                                launchSingleTop = true
                                // 重新选择先前选择的项目时恢复状态
                                restoreState = true
                            }
                        }
                    )
                }
            }
        }
    ) { innerPadding ->
        NavHost(navController,
            startDestination = Screen.Profile.route,
            Modifier.padding(innerPadding)) {
            composable(Screen.Profile.route) { Profile(navController) }
            composable(Screen.FriendsList.route) { FriendsList(navController) }
        }
    }
}

@OptIn(ExperimentalPagerApi::class)
@Composable
fun NavPageView() {
    ConstraintLayout(Modifier.fillMaxSize()) {
        val (text, page) = createRefs()
        val pageState = rememberPagerState()
        LaunchedEffect(key1 = pageState) {
            snapshotFlow { pageState.currentPage }.collect{
                Log.d("yyzzpage",it.toString())
            }
        }
        HorizontalPager(count = 2,
            state = pageState, modifier = Modifier
                .constrainAs(page) {
                    top.linkTo(parent.top)
                    bottom.linkTo(text.top)
                    height = Dimension.preferredWrapContent
                    width = Dimension.matchParent
                }
                .fillMaxSize()) { index ->
            if (index == 0) {
                Image(painter = painterResource(id = R.mipmap.icon_comment),
                    contentDescription = "",
                    modifier = Modifier.shadow(10.dp,
                        RoundedCornerShape(10.dp)))
            } else if (index == 1) {
                Text(text = "我是1")
            }
        }
        Text(text = "我是底部", modifier = Modifier
            .constrainAs(text) {
                bottom.linkTo(parent.bottom)
                height = Dimension.wrapContent
                width = Dimension.matchParent
            })
    }


}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeWeatherProjectTheme {
//        Greeting4("Android")
        NavPageView()
    }
}